package com.homedo.odin.solution.service.bizservice.pc;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.config.SysConfigHelper;
import com.homedo.odin.solution.contract.pc.SolutionHomedoListResp;
import com.homedo.odin.solution.enumcode.solution.SolutionStateEnum;
import com.homedo.odin.solution.enumcode.solution.SolutionTypeEnum;
import com.homedo.odin.solution.model.entity.BrandFirm;
import com.homedo.odin.solution.model.entity.BrandOwner;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.service.BrandFirmServiceImpl;
import com.homedo.odin.solution.service.BrandOwnerServiceImpl;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zhaoxudong
 * @date: 2018/4/4 13:39
 * @Description:
 */
@Service
public class SkuBizService {

    @Autowired
    SolutionServiceImpl solutionService;
    @Autowired
    BrandOwnerServiceImpl brandOwnerService;
    @Autowired
    BrandFirmServiceImpl brandFirmService;
    @Autowired
    SysConfigHelper sysConfigHelper;

    /**
     * 根据skuId查找方案列表
     */
    public List<SolutionHomedoListResp> solutions(String skuId) {
        if (StringUtils.isBlank(skuId)) {
            throw new BizException(Meta.CODE_451.getCode(), "skuId不能为空！");
        }
        List<SolutionHomedoListResp> responseList = new ArrayList<>();
        List<Solution> solutionList = solutionService.findListBySkuId(skuId);
        if (CollectionUtils.isNotEmpty(solutionList)) {
            SolutionHomedoListResp solutionResponse;
            for (Solution solution : solutionList) {
                solutionResponse = new SolutionHomedoListResp();
                solutionResponse.setSolutionId(solution.getSolutionId().toString());
                solutionResponse.setSolutionName(solution.getSolutionName());
                solutionResponse.setSolutionType(solution.getSolutionType());
                solutionResponse.setDescription(solution.getDescription());
                solutionResponse.setMatchPic(solution.getMatchPic());
                solutionResponse.setCreateTime(solution.getCreateTime());
                solutionResponse.setPageView(solution.getPageView());
                solutionResponse.setUserId(solution.getUserId());
                //拼接方案详情跳转地址
                String solutionDetailUrl = sysConfigHelper.getSolPageUrl()
                        .replace("@solutionId@", solution.getSolutionId().toString());
                String brandFirmUrl = null;
                if (solution.getSolutionType().intValue() == SolutionTypeEnum.BRAND_SOLUTION.getDbValue()) {
                    solutionDetailUrl += "?type=2";
                } else {
                    solutionDetailUrl += "?type=1";
                }
                solutionResponse.setSolutionDetailUrl(solutionDetailUrl);
                //增加品牌信息
                if (NumberHelper.validatePositive(solution.getBrandId())) {
                    BrandOwner brandOwner = brandOwnerService.getBrandOwnerById(solution.getBrandId());
                    if (brandOwner != null) {
                        solutionResponse.setBrandIocn(brandOwner.getBrandIcon());
                    }
                }
                //判断是否是品牌厂商
                if (NumberHelper.validatePositive(solution.getUserId())) {
                    BrandFirm brandFirm = brandFirmService.getBrandFirmByUserId(solution.getUserId());
                    if (brandFirm != null && SolutionStateEnum.AUDITSUCCESS.getDbValue() == brandFirm.getStatus()
                            .intValue()) {
                        solutionResponse.setFirmName(brandFirm.getFirmName());
                        brandFirmUrl = sysConfigHelper.getBrandFirmUrl()
                                .replace("@uid@", solution.getUserId().toString());
                    }
                }
                solutionResponse.setBrandFirmUrl(brandFirmUrl);
                responseList.add(solutionResponse);
            }
        }
        return responseList;
    }
}
